#include "widget.h"
#include "ui_widget.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QDateTime>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QVBoxLayout *layout = new QVBoxLayout(this);
    // 创建控件
    QLabel *label = new QLabel("0");
    label->setStyleSheet("font-size: 50px; color: red;");
    QLabel *label_date = new QLabel("0");
    label_date->setStyleSheet("font-size: 50px; color: blue;");

    QPushButton *startBtn = new QPushButton("开始");
    QPushButton *stopBtn = new QPushButton("停止");

    // 按钮布局
    QHBoxLayout *btnLayout = new QHBoxLayout;
    btnLayout->addWidget(startBtn);
    btnLayout->addWidget(stopBtn);

    // 主布局
    layout->addWidget(label, 0, Qt::AlignCenter);
    layout->addWidget(label_date, 0, Qt::AlignCenter);
    layout->addLayout(btnLayout);

    // 创建定时器和计数器
    QTimer *timer = new QTimer(this);

    // 连接信号与槽
    connect(timer, &QTimer::timeout, [=](){
        static int num=1;
        label->setText(QString::number(num++));
    });

    connect(timer, &QTimer::timeout, [=](){
        QString str=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        label_date->setText(str);
    });


    connect(startBtn, &QPushButton::clicked, [=](){
        timer->start(1000);
    });
    connect(stopBtn, &QPushButton::clicked, [=]{
        timer->stop();
    });
}





Widget::~Widget()
{
    delete ui;
}

